package com.hkj.genealogy.genealogydemo.controller;

import com.hkj.genealogy.genealogydemo.entity.User;
import com.hkj.genealogy.genealogydemo.service.UserServiceImp;
import com.hkj.genealogy.genealogydemo.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;


/**
 * @program: genealogydemo
 * @description: 用户控制类
 * @author: Hekejian
 * @time: 2020-07-05 00:00
 **/
@Controller
public class UserController {
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    UserServiceImp userServiceImp;
    @RequestMapping("/doRegister")
    public String doRegister(User user, Map<String,Object> map) throws Exception {
        //对密码进行加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        boolean b=true;
        try {
             IdUtil idUtil=new IdUtil(2,8,5);
             Long fid=idUtil.getId();
             Long userId=new IdUtil(3,6,1).getId();
             user.setId(userId);
             b = userServiceImp.registerNewUser(user,fid);
        }
        catch (Exception e){
            map.put("code",0);
            map.put("msg","注册失败！用户名已存在，请重试");
            return "register";
        }
        if (b) {
            map.put("code",1);
            map.put("msg","注册成功，请返回登陆");
        }
        return "register";
    }
}
